package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Autowired
    private ClazzService clazzService;

    /**
     * 学员列表查询
     *
     * @param student
     * @return
     */
    @Override
    public PageResult<Student> findAll(StudentQueryParam student) {

        IPage<Student> pageInfo = new Page<>(student.getPage(), student.getPageSize());
        LambdaQueryWrapper<Student> queryWrapper = Wrappers.<Student>lambdaQuery()
                .like(student.getName() != null, Student::getName, student.getName())
                .eq(student.getDegree() != null, Student::getDegree, student.getDegree())
                .eq(student.getClazzId() != null, Student::getClazzId, student.getClazzId());
        queryWrapper.orderByDesc(Student::getUpdateTime);
        // 调用分页
        IPage<Student> iPage = this.page(pageInfo, queryWrapper);
        long total = iPage.getTotal();
        List<Student> records = iPage.getRecords();

        // 为学生绑定clazzName
        if(!CollectionUtils.isEmpty(records)){
            // 获取所有班级id
            List<Integer> studentIds = records.stream().map(student1 -> student1.getClazzId()).toList();
            // 去重
            List<Integer> ids = studentIds.stream().distinct().toList();
            // 根据clazzId查询clazz对象集合
            List<Clazz> clazzList = clazzService.listByIds(ids);
            records.forEach(student1 -> {
                clazzList.stream().filter(clazz -> clazz.getId().equals(student1.getClazzId()))
                        .findFirst()
                        .ifPresent(clazz -> student1.setClazzName(clazz.getName()));
            });
        }

        return new PageResult<>(total, records);
    }

    /**
     * 违纪处理
     *
     * @param id
     * @param score
     */
    @Override
    @Transactional
    public void updateStudent(Integer id, Integer score) {
        // 根据id查询学员
        Student stu = this.getById(id);
        // 违纪次数加1
        stu.setViolationCount((short) (stu.getViolationCount() + 1));
        // 违纪分数+score
        stu.setViolationScore((short) (stu.getViolationScore() + score));

        this.updateById(stu);
    }
}
